<template>
	<view class="tabarIndex">
		<Drag></Drag>
		<u-toast ref="uToast" />
		<u-modal width="400px" v-model="daojushow" @confirm="confirm(true)"  :mask-close-able="true" title="道具提示">
			<view class="tips_title">尊敬的玩家您好， 当前模式需要准备如下道具：</view>
			<view class="tips_item">1. 口球、项圈、牵引绳、手铐、脚铐、羽毛棒、捆绑绳、十字扣、皮鞭、乳夹、低温蜡烛</view>
			<view class="tips_item">2. 远程震动棒（静音款）、一套制服</view>
			<view class="tips tips_item tips_title">3.作者特别为您收集了全家桶套装， 可一键下单， 长按下方图片即可， 您的每一份支持， 都将会帮助我们走的更远~</view>
			<u-image width="100%" height="380px" src="../../static/share2.jpg"></u-image>
		</u-modal>
		<u-modal width="350px" v-model="showStoreFlag" :mask-close-able="true" title="精选好物推荐">
			<view class="tips tips_item tips_title">您的每一份支持， 都将会帮助我们走的更远~</view>
			<u-image style="margin: auto;" width="310px" height="500px" src="../../static/shop1.jpg"></u-image>
		</u-modal>
		<u-modal v-model="show"   @confirm="confirm(false)"  :mask-close-able="true" title="识别码认证">
			<view class="slot-content">
				<view class="_title">一个识别码只能绑定一台设备， 如绑定多台设备， 系统会禁用识别码</view>
				<view class="_title"><a style="color: blue;" :href="site.payUrl">点击购买识别码</a></view>
				<view class="_width" style="width: 85%; margin: auto;">
					<u-input v-model="code" width="80%" placeholder="请输入识别码" :focus="true" :border="true" />
				</view>
			</view>
		</u-modal>
				
	<!-- 	<view v-if="selectTab === 0">
				<component  class="cbody" is="index"></component>
		</view> -->
		
		<view >
			<!-- <view class="header">
				{{title}}
			</view> -->
			<u-swiper @click="showStore" height="400" :list="imgs"></u-swiper>
			<!-- <view >
				<u-image width="100%" height="250px"  src="../../static/bg1.jpg"></u-image>
			</view> -->
			<view class="content">
				<view class="content_item" :style="{backgroundColor: item.backgroundColor, width: item.width}" v-for="(item,index) in modeList" :key="index+'cont'" @click="toDetail(item, index)">
					{{item.name}} <text class="go_btn">Go ></text>
				</view>
			<!-- 	
				<view class="content_item" style="width: 94%;">
					道具模式
				</view> -->
			</view>
		</view>
		<view class="tabarFooter">
			<view v-for="(item ,index) in tabList" :key="index+'tab'" @click="handleTab(index)"
				:class="index===selectTab?'tabitem  select_tab':'tabitem'">
				<!-- <image  class="icon" :src="item.img" mode=""></image> -->
				{{item.value}}
			</view>
			
			
		</view>
	</view>
</template>

<script>
	import request from '@/common/request.js';
	import Drag from '../../components/drag_demo/index'
	import Index from './feixq/index'
	import tabData from '@/utils/tab.js'
	export default {
		components: {
					Index, Drag
				},
		data() {
			return {
				title: '情侣飞行棋',
				url: '',
				code: '',
				imgs: ['../../static/bg1.jpg', '../../static/bg2.jpg', '../../static/shop2.jpg'],
				tabList: [],
				show: false,
				daojushow: false,
				showStoreFlag: false,
				modeList: [],
				selectTab: 1,
				site: {
					
				},
				codes: {
					'135677': true,
					'365889': true,
					'438954': true,
					'489288': true,
					'458000': true
				}
			}
		},
		onLoad(){
			// Mode.forEach(item=>{
			// 	if(tabData[this.selectTab].gameName===item.gameName) this.modeList=item.modeList
			// })
		},
		mounted() {
			const hostnames = ['x', 'y', 'a', 's', 'k'] 
			
			this.init_games();
			this.getWxUserInfoCode();
			this.initWeb();
		},
		methods: {
			initWeb() {
				let opts = {
					url: '/api/v1/site/host',
					method: 'get'
				};
				uni.showLoading({
					title: '加载中'
				});
				request.httpRequest(opts).then(res => {
					uni.hideLoading();
					if (res.code === "B0001") {
						this.$refs.uToast.show({
							title: res.msg,
							type: 'error'
						})
					} else {
						console.log('site', res.data)
						uni.setStorageSync("site", res.data)
						this.site = res.data;
						
						if (this.site.ad){
							this.imgs = JSON.parse(this.site.ad).map(item => item.url)
						}
					}
					
					this.tabList = tabData
					this.handleTab(0)
				});
			}, 
			showStore(index) {
				
				if (this.site.ad){
					window.location = JSON.parse(this.site.ad)[index].link;
					return;
				}
				this.showStoreFlag = true;
				
				
			},
			//微信公众号登录
			getWxUserInfoCode() {
				// var code = this.getUrlParam('code'); // 截取路径中的code，如果没有就去微信授权，如果已经获取到了就直接传code给后台获取openId
				// // var local = encodeURIComponent(window.location.href);
				// var local = encodeURIComponent("http://zhifu5.dashi88.cn");
				// let appid = 'wx98f6fdb67a4945db';
				 
				
				// if (code == null || code === '') {
				// 	window.location.href =
				// 		'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid+'&redirect_uri='+local+'&response_type=code&scope=snsapi_base&connect_redirect=1&state=#wechat_redirect';
				// } else {
				// 	const res = this.getOpenId(code); //把code传给后台获取用户信息
				// }
			},
			getOpenid(code) {
			    return new Promise(function (resolve, reject) {
			        let appid = config.appid
			        let secret = config.secret
			        let url = `https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${code}&grant_type=authorization_code`
			        wx.request({
			            url: url,
			            success: (res) => {
			                let openid = res.data.openid;
			                // console.log('openid', openid);
			                resolve(openid)
			            }
			        })
			    })
			},
			init_games(){
				let opts = {
					url: '/api/v1/mode/web_list',
					method: 'get'
				};
				uni.showLoading({
					title: '加载中'
				});
				request.httpRequest(opts).then(res => {
					uni.hideLoading();
					if (res.data) {
						tabData.forEach(tab => {
							tab.modeList = res.data.filter(item => item.gameCode === tab.gameName);
						})
					} else {
						uni.showLoading({
							title: res.msg
						});
					}
					
					this.tabList = tabData
					this.handleTab(0)
				});
			},
			confirm(flag) {
				if (this.code !== '' && this.codes[this.code.trim()]){
					document.cookie = this.code;
					
					console.log('this.daojushow', this.daojushow)
					if (this.url.indexOf("daoju") > -1 && !flag){
						this.daojushow = true;
						return;
					}
					
					uni.navigateTo({
					  url: this.url
					})
				} else {
					this.$refs.uToast.show({
						title: '错误的认证码， 请检查授权码是否正确。',
						type: 'error'
					})
					this.show = true;
				}
			},
			auth(url, mode) {
				this.url = url;
				if (!document.cookie || !this.codes[document.cookie]){
					// 弹窗， 需要认证
					this.show = true;
					return;
				}
				this.code = document.cookie;
				if (mode.code === 'daoju'){
					this.daojushow = true;
					return;
				} 
				if(mode.code==='custom'){
					// console.log('进入111')
					uni.navigateTo({
					  url: './feixq/custom/custom'
					})
				}else if(mode.code==='combination'){
					console.log('进入2')
					uni.navigateTo({
					  url:'./feixq/combination/combination'
					})
				}else{
					uni.navigateTo({
					  url: url
					})
				}
				
			},
			toDetail(mode, index){
				console.log(mode)
				if (mode.code === 'no'){
					this.$refs.uToast.show({
						title: mode.msg,
						type: 'error'
					})
					return;
				}
				if (mode.code === 'combination'){
					this.$refs.uToast.show({
						title:'当前模式暂未开放,敬请期待',
						type: 'warning'
					})
					return;
				}
				const page = tabData[this.selectTab];
				if (!page.url){
					return;
				}
				let url = '';
				if (mode.id){
					url = page.url +`?item=${mode.id}`;
				} else {
					url = page.url +`?item=null`;
				}
				
				if (index > 0){
					// 需要输入密码框
					this.auth(url, mode);
				} else {
					uni.navigateTo({
					  url: url
					})
				}
			},
			handleTab(index) {
				this.selectTab = index
				this.modeList = tabData[index].modeList
			},
			changeTabar(index) {},
		}
	}
</script>

<style lang="scss" scoped>
	._title{
		margin: 5px;
		font-size: 18px;
		text-align: center;
		font-weight: bold;
		// color: #fff;
	}
	.tips_title{
		font-size: 17px;
		font-weight: bold;
	}
	.tips_item{
		font-size: 17px;
		margin-top: 5px;
	}
	body::-webkit-scrollbar{
			display: none;
		}
		
	.tabarIndex {
		background-color: #e0e0e0;
		height: 100vh;

		.content {
			display: flex;
			flex-wrap: wrap;
			align-items: center;
			padding: 15rpx;

			&_item {
				margin-bottom: 30rpx;
				width: 45%;
				// margin-left: 5%;
				margin: auto;
				margin-top: 20rpx;
				height: 150rpx;
				border-radius: 10rpx;
				background-color: #ffffff;
				text-align: center;
				line-height: 150rpx;
				font-size: 40rpx;
				color: #fff;
				font-weight: bold;
				box-shadow: 3px 3px 5px #b3b1b1;
				.go_btn {
					height: 30rpx;
					width: 80rpx;
					color: #fff;
					// background-color: #41a1f1;
					border-radius: 30rpx;
					font-size: 12rpx;
					padding: 4rpx 8rpx;
					line-height: 30rpx;
					margin-left: 10rpx
				}

			}

			// &_item:nth-child(2) {
			// 	color: #fdb24c;
			// 	background-color: #fff4e1;

			// 	.go_btn {
			// 		background-color: #fdb24c;
			// 	}
			// }

			// &_item:nth-child(3) {
			// 	background-color: #ffebf0;
			// 	color: #eb83a1;

			// 	.go_btn {
			// 		background-color: #eb83a1;
			// 	}
			// }

			// &_item:nth-child(4) {
			// 	background-color: #5765b4;
			// 	color: red;
			// 	.go_btn {
			// 		background-color: red;
			// 	}
			// }

			// &_item:nth-child(5) {
			// 	background-color: #27ab60;
			// 	color: #5765b4;

			// 	.go_btn {
			// 		background-color: #5765b4;
			// 	}
			// }
		}

		.tabarFooter {
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			display: flex;
			height: 100rpx;
			background-color: #fff;
			z-index: 100;
			.tabitem {
				width: 33%;
				display: flex;
				font-size: 26rpx;
				flex-direction: column;
				align-items: center;
				justify-content: center;

				.icon {
					width: 80rpx;
					height: 80rpx;
				}
			}

			.select_tab {
				color: #f4c45d;
			}
		}
	}
</style>